disp('======= KITTI 2015 Benchmark Evaluation =======');
clear all; close all; dbstop error;

% error threshold
tau = [3 0.05];
est_path='/home/chu/disp/ucl/stereo_2015';
splited_path=split(est_path,'/');
tmp=strcat(splited_path(length(splited_path)),'.txt');
tmp=tmp{1};
ftmp=fopen(tmp,'w');
fname=sprintf('%s/%s',est_path,'error.txt');
f=fopen(fname,'w');
fprintf(f,'%s%d%s\n','epe threshold: ',tau(1),' pixels');
fprintf(ftmp,'%s%d%s\n','epe threshold: ',tau(1),' pixels');

save_error_map=0;
show_results_fig=0;

for i=0:199
    
    display_content1=sprintf('%s%03d%s','Calculating for left disparity map ',i, '... ');
    disp(display_content1);
    
    D_est = disp_read(sprintf('%s%s%06d%s',est_path,'/',i,'.png'));
    D_gt  = disp_read(sprintf('%s%06d%s','/home/chu/scene_flow/scene_flow_2015/training/disp_noc_0/',i,'_10.png'));
    d_err = disp_error(D_gt,D_est,tau);
    D_err = disp_error_image(D_gt,D_est,tau);
    
    display_content2=sprintf('%03d %.2f%s\n',i,d_err*100,'%');
    disp(display_content2);
    
    fprintf(f,'%06d %.2f%s\n',i,d_err*100,'%');
    fprintf(ftmp,'%06d %.2f%s\n',i,d_err*100,'%');
    
    if save_error_map;
        imwrite(D_err,sprintf('%s/%s%06d%s',est_path,'error',i,'.png'));
    end
    if show_results_fig;
        figure,imshow([disp_to_color([D_est;D_gt]);D_err]);
        title(sprintf('Disparity Error: %.2f %%',d_err*100));
    end
end

fclose(f);
fclose(ftmp);